package game.core
{
	import game.*;
	import flash.display.*;
	import engine.core.*;
	import engine.math.*;
	import engine.asset.*;
	import engine.graphics.vfx.*;
	import game.network.*;
	import game.actor.*;
	import game.messages.inventory.*;
	import game.scene.*;
	import game.graphics.*;
	import game.ui.core.*;
	import game.messages.store.*;
	import game.messages.simulation.*;
	import game.graphics.away3d.*;
	import engine.social.*;
	import flash.net.*;
	import engine.graphics.lighting.*;
	import game.graphics.lighting.*;
	import game.store.*;
	import game.messages.progression.*;
	import game.quest.*;
	//import game.messages.viewer.*;
	import game.messages.quest.*;
	import game.messages.login.*;
	import game.localization.*;

	public class RxGameCommandConsole extends RxCommandConsole
	{
		private var m_inviteName:String;
		private static var s_stage:Stage;
		private static const gigaTextureUsage:String = "Specify 0 or 1 to disable or enable debug giga texture";
		private static const masterVolumeUsage:String = "Specify a number in the range [0.0, 1.0] to scale the master volume";
		private static const custDescription:String = "Customize player character";
		private static const custUsage:String = " cust skel <name> -- to specify the skeleton\n" + " cust anim <name> -- to specify the animation\n" + " cust slot <name> <mesh> <texture> <r g b a> -- to specify the slot description\n" + " cust clear -- clears all slots\n" + " cust assemble -- assembles all specified parts\n\n";
		private static const resetCharacterUsage:String = "Reset your character to its initial state upon next login";
		private static const resetOptionsUsage:String = "Reset your local option settings";
		private static const inventoryUsage:String = "Show player character\'s inventory";
		private static const assetDescription:String = "Display information on loaded assets";
		private static const assetUsage:String = " asset list       - lists all asset types\n" + " asset list type  - lists all loaded assets of specified type\n" + " asset list all   - lists all loaded assets\n";
		private static const entityDescription:String = "Display information on instanced entities";
		private static const entityUsage:String = " entity list       - lists all entity types\n" + " entity list type  - lists all instanced entities of specified type\n" + " entity list all   - lists all instanced entities\n";
		private static const netDebugUsage:String = "Add a debug message to the log";
		private static const resetMapDescription:String = "reset the map used on the next level start";
		private static const resetMapUsage:String = "resetMap <map name>";
		private static const mapNameDescription:String = "show the current map name";
		private static const mapNameUsage:String = "mapName";
		private static const listFriendsDescription:String = "print a list of your friends";
		private static const listFriendsUsage:String = "listFriends";
		private static const inviteDescription:String = "invite a player into your party";
		private static const inviteUsage:String = "invite <friend name>";
		private static const uninviteDescription:String = "remove a player from your party";
		private static const uninviteUsage:String = "uninvite <friend name>";
		private static const listInvitesDescription:String = "list your party invitations";
		private static const listInvitesUsage:String = "listinvites\n";
		private static const acceptInviteDescription:String = "accept a party invitation";
		private static const acceptInviteUsage:String = "acceptinvite <friend>\n";
		private static const resetInvitesDescription:String = "reset all invites and parties";
		private static const resetInvitesUsage:String = "resetinvites <message>\n";
		private static const leavePartyDescription:String = "leave your party";
		private static const leavePartyUsage:String = "leaveparty";
		private static const teleportDescription:String = "teleport to a friend";
		private static const teleportUsage:String = "teleport <friend>";
		private static const reloadAssetsUsage:String = "reload <type> -- reloads all assets of the specified type";
		private static const debugRenderDescription:String = "Debug rendering to show gbuffer channels, lighting and blurred buffers";
		private static const debugRenderUsage:String = " debugrender -- display final lit results\n" + " debugrender depth -- display gbuffer depth channel\n" + " debugrender normals -- display gbuffer normals channel\n" + " debugrender lighting -- display the lighting\n";
		private static const postFXDescription:String = "Post FX settings";
		private static const postFXUsage:String = " postfx bloomScale <scale> -- the amount of bloom\n" + " postfx baseWeight <weight> -- the weight of the base color\n" + " postfx bloomWeight <weight> -- the weight of the bloom color\n" + " postfx blurChannelWeights <r g b a> -- the weight of the blur color in each color channel\n" + " postfx toneCurveSlopes <start end> -- tone cubic curve start and end slopes\n" + " postfx brightness <adjust> -- the final brightness adjustment\n" + " postfx contrast <adjust> -- the final contrast adjustment\n" + " postfx saturation <adjust> -- the final saturation adjustment\n" + " postfx tone0 <r g b> -- the dark tone used when desaturated\n" + " postfx tone1 <r g b> -- the bright tone used when desaturated\n" + " postfx blur <0/1> -- toggles the blur effect\n" + " postfx gaussianDownsamples <kernel> -- sets the Gaussian filter kernel used by downsamples\n" + " postfx gaussianBlurs <kernel> -- sets the Gaussian filter kernel used by blurs\n" + " postfx downsamples <count> -- sets the number of downsamples used in bloom blur\n";
		private static const playerNameUsage:String = "playerName <display name for your player>";
		private static const setPlayerLevelUsage:String = "setplayerlevel <desired level>";
		private static const spawnVFXDescription:String = "Spawns a named vfx at the hero\'s position";
		private static const spawnVFXUsage:String = " vfx <name> -- spawns the named vfx\n";
		private static const playSeqDescription:String = "Plays a sequence of the hero";
		private static const playSeqUsage:String = " seq <name> -- play the named sequence\n";
		private static const pointLightDescription:String = "Add a random point light at the hero\'s position";
		private static const pointLightUsage:String = " pointlight clear -- removes all point lights\n" + " pointlight add -- adds a random point light at the hero\'s position\n";
		private static const questDescription:String = "Quest debug commands";
		private static const questUsage:String = " quest [accept|complete] <questId> -- use live flow to attempt to accept or complete a quest\n" + " quest inctask <questId> <task#> -- increment a quest\'s task by 1\n" + " quest setstate <questId> [hidden|avail|inprog|pend|comp] -- debug force quest to a state\n" + " quest status -- requests status update on all quest states\n" + " quest clearallstates -- clears all quest states for this user\n" + " quest checkrewards <questId> -- outputs gold & xp rewards for completing the specified quest\n";
		private static var s_questDebugListenerActive:Boolean = false;
		private static const heroScaleDescription:String = "Scale the hero";
		private static const heroScaleUsage:String = " heroScale <scale> -- scales the hero by the specified scale\n";
		private static const profileDescription:String = "Toggle display of profilers";
		private static const profileUsage:String = " profile <none|frame|min|accum|sampling> -- toggle display of profilers between none, per-frame profilers, minimal per-frame profilers, accumulated profilers, and sampling profilers\n";
		private static const infoDescription:String = "Display info on system and game state";
		private static const infoUsage:String = "Display info on system and game state";
		private static const cameraDescription:String = "Set camera position";
		private static const cameraUsage:String = "Set camera position";
		private static const animDescription:String = "Toggle animation of characters";
		private static const animUsage:String = " anim <0|1> -- toggle animation of characters\n";
		private static const localeDescription:String = "A set of commands used to manage locale";
		private static const localeUsage:String = " Passing no parameters displays current locale\n" + " set -- changes the locale.  i.e: locale set en_us\n" + " getstring -- gets a string in the current locale\n" + " usage -- Displays this text\n";
		private static const screenCaptureDescription:String = "Screen capture the next rendered frame";
		private static const screenCaptureUsage:String = " screencapture <3d|2d> -- Screen capture the next rendered frame.  Optionally specify 3d or 2d to only capture stage2D or stage3D";
		private static const tutorialDescription:String = "Runs a tutorial script";
		private static const tutorialUsage:String = "Runs the specified tutorial script.\n" + " tutorial <script name> - ie: tutorial newgame.xml";
		private static const storeDescription:String = "Store debugging functions";
		private static const storeUsage:String = " store list : list aisles, offers, and items available\n" + " store buy # : requests a purchase of the offer at the specified index\n";
		private static const dialogDescription:String = "Runs the specified dialog sequence";
		private static const dialogUsage:String = "Runs the specified dialog script sequence.  The id must be specified in the questtext.csv file\n" + " dialog <dialogId>";
		private static const renderDescription:String = "Toggle various rendering features on and off";
		private static const renderUsage:String = " render <0|1> -- toggles all rendering and rendering related updates.\n" + " render updates <0|1> -- toggles render updates, including visibility, animation, and vfx animation.\n" + " render drawcalls <0|1> -- toggles low-level drawcalls, which effectively disables GPU.\n" + " render rendercalls <0|1> -- toggles high-level render calls, affecting everything, including models, meshes, vfx, and lights.\n" + " render lighting <0|1> -- toggles deferred lighting.\n" + " render vfx <0|1> -- toggles vfx updates and rendering.\n" + " render postfx <0|1> -- toggles the post fx.\n" + " render aa <0|2|4|16> -- sets the anti-aliasing mode.\n" + " render quality <0|1|2|4> -- sets the graphics quality.\n";
		private static const hudDescription:String = "Provides access to various hud features and configurations";
		private static const hudUsage:String = "hud usage:\n" + " hud unlockmaps -- unlocks all locations in the world map\n" + " hud loadfriends -- loads friends list";
		private static const clientVarsDescription:String = "Command to modify clientvars";
		private static const clientVarsUsage:String = "var <name> <value> -- sets the clientvar with the given name to the specified value.\n";
		private static const workerDescription:String = "Command to toggle worker mode between asynchronous and synchronous";
		private static const workerUsage:String = " worker <async|sync> -- toggles the worker mode between asynchronous and synchronous.\n";
		
		public function RxGameCommandConsole(style:String, stage:Stage)
		{
			s_stage = stage;
			
			super(style, stage);
			
			SetCommandFunction("info", infoDescription, infoUsage, new RxCallback(this.infoCommand));
			SetCommandFunction("resetCharacter", resetCharacterUsage, resetCharacterUsage, new RxCallback(this.ResetCharacterCommand));
			SetCommandFunction("resetmap", resetMapDescription, resetMapUsage, new RxCallback(this.ResetMapCommand));
			SetCommandFunction("setplayerlevel", setPlayerLevelUsage, setPlayerLevelUsage, new RxCallback(this.SetPlayerLevelCommand));
			SetCommandFunction("store", storeDescription, storeUsage, new RxCallback(this.StoreCommand));
		}
		
		private function infoCommand(param1:Array) : void
		{
			var _loc_2:* = RxSystemInfo.Write(s_stage);
			Write(_loc_2);
			var _loc_3:* = RxGameInfo.Write();
			Write(_loc_3);
		}
		
		private function ResetCharacterCommand(param1:Array) : void
		{
			var _loc_2:* = new RxLogin_ResetCharacterMessage();
			RxTransactionServer.instance.SendMessage(_loc_2);
			Write("INFO: character marked for reset upon next login\n");
		}
		
		private function ResetMapCommand(param1:Array) : void
		{
			var _loc_2:String = null;
			if (param1.length > 1)
			{
				_loc_2 = param1[1];
			}
			else
			{
				_loc_2 = RxMaster.GetMapName();
				if (_loc_2 == null)
				{
					_loc_2 = RxProgression_Constants.s_firstMapName;
				}
			}
			var _loc_3:* = RxAssetCSV.GetByName("levels");
			if (_loc_3.RowExists(_loc_2))
			{
				RxSimulation.SetNextMap(_loc_2);
			}
			else
			{
				Write("ERROR: Map name \'" + _loc_2 + "\' not recognized.  Make sure the name is added to levels.csv.\n");
			}
		}
		
		private function SetPlayerLevelCommand(param1:Array) : void
		{
			if (param1.length != 2)
			{
				Write(setPlayerLevelUsage);
				return;
			}
			var _loc_2:* = new RxSimulation_DebugSetPlayerLevel();
			_loc_2.level = param1[1];
			RxTransactionServer.instance.SendMessage(_loc_2);
		}
		
		private function StoreCommand(param1:Array) : void
		{
			var _loc_3:RxStore_PaywallEventMessage = null;
			var _loc_4:uint = 0;
			var _loc_5:uint = 0;
			var _loc_6:RxStore_AisleInfo = null;
			var _loc_7:RxStore_OfferInfo = null;
			var _loc_8:String = null;
			var _loc_9:int = 0;
			var _loc_10:int = 0;
			var _loc_11:RxStore_DebugGrantCurrency = null;
			var _loc_2:Boolean = true;
			if (param1.length > 1)
			{
				switch(param1[1].toLowerCase())
				{
					case "list":
					{
						_loc_2 = false;
						RxStore.instance.RetrieveStoreFromServer(this.RetrieveStoreCallback);
						break;
					}
					case "buy":
					{
						if (param1.length > 2)
						{
							_loc_4 = uint(param1[2]);
							_loc_5 = 0;
							for each (_loc_6 in RxStore.instance.GetAisles())
							{
								
								for each (_loc_7 in _loc_6.offers)
								{
									
									if (++_loc_5 == _loc_4)
									{
										_loc_2 = false;
										Write("store buy: purchase request submitted for offer #" + _loc_4 + ":\n");
										this.PrintOffer(_loc_7, _loc_4, RxDate.Now());
										RxStore.instance.RequestPurchase(_loc_7, "RxGameCommandConsole", this.PurchaseCallback);
									}
								}
							}
							if (_loc_2)
							{
								Write("store buy: purchase failed: invalid offer index\n");
							}
						}
						break;
					}
					case "grantmoney":
					{
						if (param1.length >= 4)
						{
							_loc_8 = param1[2];
							_loc_9 = int(param1[3]);
							_loc_10 = RxStore.GetCurrencyFromString(_loc_8);
							if (_loc_10 >= 0 && _loc_9 > 0)
							{
								_loc_11 = new RxStore_DebugGrantCurrency();
								_loc_11.amount = _loc_9;
								_loc_11.currency = _loc_10;
								RxTransactionServer.instance.SendMessage(_loc_11);
								_loc_2 = false;
							}
						}
						break;
					}
					case "paywallevent":
					{
						_loc_3 = new RxStore_PaywallEventMessage();
						RxTransactionServer.instance.SendMessage(_loc_3);
						_loc_2 = false;
						break;
					}
					default:
					{
						break;
					}
				}
			}
			if (_loc_2)
			{
				Write(storeUsage);
			}
		}
		
		private function RetrieveStoreCallback(param1:RxStore) : void
		{
			var _loc_4:RxStore_AisleInfo = null;
			var _loc_5:RxStore_OfferInfo = null;
			var _loc_2:* = RxDate.Now();
			var _loc_3:uint = 0;
			for each (_loc_4 in param1.GetAisles())
			{
				
				Write("  Aisle \"" + _loc_4.name + "\":\n");
				Write("    expires = " + GetDateString(_loc_4.endTime, _loc_2) + "\n");
				for each (_loc_5 in _loc_4.offers)
				{
					
					this.PrintOffer(_loc_5, ++_loc_3, _loc_2);
				}
			}
		}
		
		private function PurchaseCallback(param1:RxStore, param2:RxStore_PurchaseResponseMessage) : void
		{
			var _loc_3:String = "failed for an unknown reason";
			switch(param2.response)
			{
				case RxStore_PurchaseResponseMessage.RESPONSE_SUCCESS:
				{
					_loc_3 = "succeeded";
					break;
				}
				case RxStore_PurchaseResponseMessage.RESPONSE_FAILURE_INSUFFICIENT_FUNDS:
				{
					_loc_3 = "failed: insufficient funds";
					break;
				}
				case RxStore_PurchaseResponseMessage.RESPONSE_FAILURE_INVALID_OFFER:
				{
					_loc_3 = "failed: invalid offer";
					break;
				}
				case RxStore_PurchaseResponseMessage.RESPONSE_FAILURE_OFFER_EXPIRED:
				{
					_loc_3 = "failed: offer expired";
					break;
				}
				case RxStore_PurchaseResponseMessage.RESPONSE_FAILURE_INSUFFICIENT_CAPACITY:
				{
					_loc_3 = "failed: insufficient capacity (inventory)";
					break;
				}
				default:
				{
					break;
					break;
				}
			}
			Write("store buy: purchase request " + _loc_3 + "\n");
		}
		
		public static function GetDateString(param1:Number, param2:Number) : String
		{
			/*
			var _loc_3:int = 0;
			if (IsValid(param1))
			{
				_loc_3 = GetSecondsFromAUntilB(param2, param1);
				return ConvertSecondsToString(_loc_3);
			}
			return "";
			*/
			return "";
		}
		
		private function PrintOffer(param1:RxStore_OfferInfo, param2:uint, param3:Number) : void
		{
			/*
			var _loc_7:RxInventory_ItemDescription = null;
			var _loc_8:RxItemRecord = null;
			var _loc_9:String = null;
			var _loc_10:int = 0;
			Write("    Offer #" + param2 + ":\n");
			Write("      id = " + param1.offerId + "\n");
			Write("      id = " + param1.bundleName + "\n");
			var _loc_4:* = RxStore.GetLabelForCurrency(param1.currency);
			Write("      price = " + param1.price + " " + _loc_4 + "\n");
			Write("      original price = " + param1.originalPrice + " " + _loc_4 + "\n");
			Write("      offer ends = " + GetDateString(param1.endTime, param3) + "\n");
			Write("      new until = " + GetDateString(param1.newUntilTime, param3) + "\n");
			var _loc_5:String = "no";
			if (RxDate.IsValid(param1.newUntilTime))
			{
				if (RxDate.IsABeforeB(param3, param1.newUntilTime))
				{
					_loc_5 = "yes";
				}
			}
			Write("      new = " + _loc_5 + "\n");
			_loc_5 = param1.flags & RxStore_OfferInfo.FLAG_BESTSELLER ? ("yes") : ("no");
			Write("      bestSeller = " + _loc_5 + " (flags:" + param1.flags + ")\n");
			var _loc_6:uint = 0;
			for each (_loc_7 in param1.items)
			{
				
				_loc_8 = new RxItemRecord();
				_loc_8.UpdateFromDescription(_loc_7);
				_loc_9 = _loc_8.GetBaseDefinition();
				_loc_10 = _loc_8.GetQuantity();
				Write("      item " + ++_loc_6 + ": " + (_loc_9 != null ? (_loc_9) : ("null")) + " x " + _loc_10 + "\n");
			}
			*/
		}
	}
}